# -*- coding:utf-8 -*-

from wtforms.validators import DataRequired, Length, Email, EqualTo, Regexp
from wtforms import StringField, SubmitField, PasswordField, BooleanField
from flask_wtf import FlaskForm
from ..models import User
from wtforms.validators import ValidationError


class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Length(1, 64), Email()])
    password = PasswordField('密码', validators=[DataRequired()])
    remember_me = BooleanField('记住我')
    submit = SubmitField('登录')


class RegisterForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Length(1, 64), Email()])
    username = StringField('用户名', validators=[DataRequired(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', message='只能包含数字、字母和下划线')])
    password = PasswordField('密码', validators=[DataRequired(), Length(6, 20, message='长度范围6-20位'), EqualTo('password2', message='密码不一致')])
    password2 = PasswordField('密码确认', validators=[DataRequired()])
    submit = SubmitField('注册')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('该邮箱已注册,请更换!')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('该用户名已注册,请更换!')





